package wizards;

import wizards.actions.ActorAdder;
import wizards.actions.ActorRemover;
import wizards.actors.Actor;
import wizards.actors.Creature;
import wizards.actors.Spell;
import wizards.actors.Wizard;
import wizards.controllers.NullController;
import wizards.controllers.WizardController;
import wizards.maps.WizardMap;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 * Handles the painting.
 * 
 * User: mbs207
 * Date: 4/7/11
 * Time: 8:33 AM
 */
public class WizardPanel extends JPanel {
    ArrayList<Actor> wizards = new ArrayList<Actor>();
    ArrayList<Actor> spells = new ArrayList<Actor>();
    ArrayList<Actor> creatures = new ArrayList<Actor>();
    WizardMap map;
    WizardController player_control =new NullController();
    //BufferedImage shadow = new BufferedImage(600,600, BufferedImage.TYPE_INT_ARGB);
    public WizardPanel(WizardMap m){
        map = m;

    }
    
    @Override
    public void paintComponent(Graphics g){

        map.paint(g);

        for(Actor a: wizards)
            a.paint(g);

        for(Actor a: spells)
            a.paint(g);


        for(Actor a: creatures)
            a.paint(g);

        map.paintHigh(g);

        player_control.drawStatus(g);

        //g.drawImage(shadow,0,0,this);


    }



    public void addWizard(Wizard w){
        addActor(w,wizards);
    }

    public void removeWizard(Wizard w){

        removeActor(w,wizards);
    }

    public void addSpell(Spell s){
        addActor(s, spells);
    }

    public void removeSpell(Spell s){
        removeActor(s,spells);
    }

    public void addActor(Actor a, ArrayList<Actor> list){
        EventQueue.invokeLater(new ActorAdder(a,list,map));
    }

    public void removeActor(Actor a, ArrayList<Actor> list){
        EventQueue.invokeLater(new ActorRemover(a,list,map));
    }

    public void addCreature(Creature c) {
        addActor(c,creatures);
    }

    public void removeCreature(Creature c) {
        removeActor(c,creatures);
    }

    public void setMap(WizardMap m){
        final WizardMap fm = m;
        EventQueue.invokeLater(new Runnable(){
           public void run(){
               map = fm;
           }
        });
    }
    public void setPlayerController(WizardController wc){
        player_control = wc;
        addMouseListener(player_control);
        addMouseMotionListener(player_control);
    }

    public void removePlayerController(){
        removeMouseListener(player_control);
        removeMouseMotionListener(player_control);
        player_control = new NullController();
    }
    public void reset(){
        EventQueue.invokeLater(new Runnable(){

            public void run() {
                wizards.clear();
                spells.clear();
                creatures.clear();
            }
        });

    }
}

